package com.zzt.book.controller;

import com.zzt.book.constant.Constants;
import com.zzt.book.model.UserInfo;
import com.zzt.book.service.UserInfoService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description
 * User: panda
 * Date: 2025-02-21
 * Time: 14:51
 */
@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     *
     * @param name
     * @param password
     * @param session
     * @return true 表示密码正确 false 表示密码错误
     */
    @RequestMapping("/login")
    public Boolean login(String name, String password, HttpSession session){
        log.info("username: "+ name);
        //1.参数校验
        //2.检验账号和密码是否正确
        //3.设置session
        //4.返回结果
        if(!StringUtils.hasLength(name) || !StringUtils.hasLength(password)){
            return false;
        }

        //根据用户名，获取用户信息
        UserInfo userInfo = userInfoService.queryUserInfoByName(name);
        if(userInfo == null){
            return false;
        }
        if(password.equals(userInfo.getPassword())){
            userInfo.setPassword("");
            session.setAttribute(Constants.SESSION_USER_KEY, userInfo);
            return true;
        }
        return false;
    }
}
